package leetcode.leetcode1008;

/**
 * @author czt
 * @version 1.0
 * @since 2025/1/2
 */
public class Leetcode1008 {
    public TreeNode bstFromPreorder(int[] preorder) {
        TreeNode root = new TreeNode(preorder[0]);
        for(int i=1; i<preorder.length; i++){
            int val = preorder[i];
            insert(root,val);
        }
        return root;
    }

    private TreeNode insert(TreeNode node, int val){
        if(node == null){
            return new TreeNode(val);
        }
        if(val < node.val){
            node.left = insert(node.left,val);
        }else if(val > node.val){
            node.right = insert(node.right,val);
        }
        return node;
    }
}
